<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Admin;
use App\Models\System;
use App\Models\Official;
use App\Models\MealCompany;
use App\Services\OSS;

class SystemController extends Controller
{
    //

    public function add(Request $request)
    {
    	if($request->input('admin_id',false)){
    		$userinfo = Admin::where('admin_id',$request->input('admin_id'))->first();
    		if($userinfo->power!='*'){
    			$power = json_decode($userinfo->power,true);
    		}else{
    			$power = ['*'];
    		}
    		return view('system.add',['userinfo'=>$userinfo,'power'=>$power,'power_name'=>$userinfo->power_name,'status'=>$userinfo->status,'region'=>$userinfo->region]);
    	}
    	return view('system.add',['power'=>[],'power_name'=>'','status'=>'','region'=>'']);
    }

    // 
    public function addAction(Request $request)
    {
    	$post = $request->all();
        $admin = new Admin;
        if($post['region'] == 3){
            $post['power'] = ['/shopList','/company/take','/extractList'];
            $res = $admin->add($post);
            if(!$res['success']){
                return response()->json($res);
            }
            $company = new MealCompany;
            $post['admin_id'] = $res['data']['admin_id'];
            return response()->json($company->add($post));
        }
    	return response()->json($admin->add($post));
    }

    //
    public function list(Request $request)
    {
    	$admins = Admin::orderBy('insert_time','asc')->paginate(10);
    	return view('system.list',['admins'=>$admins]);
    }

    /**
     * 活动设置
     */
    public function huodongSet(Request $request)
    {
    	$system = new System;
    	$systemInfo = $system->where('option','wx_share_quota')->first();
    	return view('system.huodong',['info'=>explode('|',$systemInfo->value)]);
    }

    /**
     * 官网编辑
     */
    public function official(Request $request)
    {
    	$official = new Official;
    	$logo = $official->where('field_name','logo')->value('value');
    	$propaganda = $official->where('field_name','propaganda')->value('value');
    	$aboutus = $official->where('field_name','aboutus')->value('value');
    	$aboutimg = $official->where('field_name','aboutimg')->value('value');
    	$aboutming = json_decode($official->where('field_name','aboutming')->value('value'),true);
    	$cardinfo = json_decode($official->where('field_name','cardinfo')->value('value'),true);
    	$cardspecial = json_decode($official->where('field_name','cardspecial')->value('value'),true);
    	$joinin = json_decode($official->where('field_name','joinin')->value('value'),true);
    	$homeimgs = json_decode($official->where('field_name','homeimgs')->value('value'),true);
    	$url = "https://jiashilexiang.oss-cn-beijing.aliyuncs.com/Official";
    	// dd();
    	return view('appmanage.official',compact('logo','propaganda','aboutus','aboutimg','aboutming','cardinfo','cardspecial','joinin','homeimgs','url'));
    }

    /**
     * 
     */
    public function editOfficial(Request $request)
    {
    	$post = $request->all();
    	$official = new Official;
    	$data = [];
    	if($request->hasFile('logo')){
    		$logoimg = $request->file('logo');
            $data['logo'] = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$logoimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$data['logo'],$logoimg);
    	}
    	if($request->hasFile('imagehome1')){
    		$aboutimgs = $official->where('field_name','homeimgs')->first();
    		if(empty($aboutimgs->value)){
    			$imgs = [];
    		}else{
    			$imgs = json_decode($aboutimgs->value,true);
    		}
    		$homeimg = $request->file('imagehome1');
            $homes = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$homeimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$homes,$homeimg);
            $imgs['imagehome1'] = $homes;
            $data['homeimgs'] = json_encode($imgs);
    	}
    	if($request->hasFile('imagehome2')){
    		$aboutimgs = $official->where('field_name','homeimgs')->first();
    		if(empty($aboutimgs->value)){
    			$imgs = [];
    		}else{
    			$imgs = json_decode($aboutimgs->value,true);
    		}
    		if(!empty($data['homeimgs'])){
    			$imgs = json_decode($data['homeimgs'],true);
    		}
    		$homeimg = $request->file('imagehome2');
            $homes = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$homeimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$homes,$homeimg);
            $imgs['imagehome2'] = $homes;
            $data['homeimgs'] = json_encode($imgs);
    	}
    	if($request->hasFile('aboutimg')){
    		$homeimg = $request->file('aboutimg');
            $homes = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$homeimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$homes,$homeimg);
            $data['aboutimg'] = $homes;
    	}
    	$data['aboutus'] = $post['aboutus'];
    	$mingju = [];
    	foreach ($post['juzi'] as $key => $value) {
    		$mingju[] = ['juzi'=>$value,'zuozhe'=>$post['zuozhe'][$key]];
    	}
    	$data['aboutming'] = json_encode($mingju);
    	$cardimgs = $official->where('field_name','cardinfo')->first();
    	if(empty($cardimgs->value)){
    		$cardinfos = [];
    	}else{
    		$cardinfos = json_decode($cardimgs->value,true);
    	}
    	if($request->hasFile('cardimg1')){
    		$cardimg = $request->file('cardimg1');
            $card = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cardimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$card,$cardimg);
            $cardinfos['card1']['img'] = $card;
            $data['cardinfo'] = json_encode($cardinfos);
    	}
    	$cardinfos['card1']['info'] = $post['aboutus1'];
    	$cardinfos['card1']['name'] = $post['cardname1'];
    	$cardinfos['card1']['price'] = $post['cardprice1'];
    	if($request->hasFile('cardimg2')){
    		$cardimg = $request->file('cardimg2');
            $card = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cardimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$card,$cardimg);
            $cardinfos['card2'] = ['img'=>$card,'info'=>$post['aboutus2'],'name'=>$post['cardname2'],'price'=>$post['cardprice2']];
            $data['cardinfo'] = json_encode($cardinfos);
    	}
    	$cardinfos['card2']['info'] = $post['aboutus2'];
    	$cardinfos['card2']['name'] = $post['cardname2'];
    	$cardinfos['card2']['price'] = $post['cardprice2'];
    	if($request->hasFile('cardimg3')){
    		$cardimg = $request->file('cardimg3');
            $card = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cardimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$card,$cardimg);
            $cardinfos['card3'] = ['img'=>$card,'info'=>$post['aboutus3'],'name'=>$post['cardname3'],'price'=>$post['cardprice3']];
            $data['cardinfo'] = json_encode($cardinfos);
    	}
    	$cardinfos['card3']['info'] = $post['aboutus3'];
    	$cardinfos['card3']['name'] = $post['cardname3'];
    	$cardinfos['card3']['price'] = $post['cardprice3'];
    	if($request->hasFile('cardimg4')){
    		$cardimg = $request->file('cardimg4');
            $card = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cardimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$card,$cardimg);
            $cardinfos['card4'] = ['img'=>$card,'info'=>$post['aboutus4'],'name'=>$post['cardname4'],'price'=>$post['cardprice4']];
            $data['cardinfo'] = json_encode($cardinfos);
    	}
    	$cardinfos['card4']['info'] = $post['aboutus4'];
    	$cardinfos['card4']['name'] = $post['cardname4'];
    	$cardinfos['card4']['price'] = $post['cardprice4'];
    	if($request->hasFile('cardimg5')){
    		$cardimg = $request->file('cardimg5');
            $card = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cardimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$card,$cardimg);
            $cardinfos['card5'] = ['img'=>$card,'info'=>$post['aboutus5'],'name'=>$post['cardname5'],'price'=>$post['cardprice5']];
            $data['cardinfo'] = json_encode($cardinfos);
    	}
    	$cardinfos['card5']['info'] = $post['aboutus5'];
    	$cardinfos['card5']['name'] = $post['cardname5'];
    	$cardinfos['card5']['price'] = $post['cardprice5'];
    	if($request->hasFile('cardimg6')){
    		$cardimg = $request->file('cardimg6');
            $card = '/'.date('Ymd').'/'.md5(uniqid(microtime(true),true)).'.'.$cardimg->getClientOriginalExtension();
            OSS::publicUpload(config('aliyun.BucketName'),'Official'.$card,$cardimg);
            $cardinfos['card6'] = ['img'=>$card,'info'=>$post['aboutus6'],'name'=>$post['cardname6'],'price'=>$post['cardprice6']];
            $data['cardinfo'] = json_encode($cardinfos);
    	}
    	$cardinfos['card6']['info'] = $post['aboutus6'];
    	$cardinfos['card6']['name'] = $post['cardname6'];
    	$cardinfos['card6']['price'] = $post['cardprice6'];
    	$data['cardinfo'] = json_encode($cardinfos);
    	$pecials = $official->where('field_name','cardspecial')->first();
    	if(empty($pecials->value)){
    		$pecials = [];
    	}else{
    		$pecials = json_decode($pecials->value,true);
    	}
    	$pecials['pecial1'] = $post['pecial1'];
    	$pecials['pecial2'] = $post['pecial2'];
    	$pecials['pecial3'] = $post['pecial3'];
    	$pecials['pecial4'] = $post['pecial4'];
    	$data['cardspecial'] = json_encode($pecials);
    	$manager = $official->where('field_name','joinin')->first();
    	if(empty($manager->value)){
    		$manager = [];
    	}else{
    		$manager = json_decode($manager->value,true);
    	}
    	$manager['citymen']  = $post['citymen'];
    	$manager['areamen']  = $post['areamen'];
    	$manager['provincemen']  = $post['provincemen'];
    	$data['joinin']  = json_encode($manager);
    	return response()->json($official->edit($data));
    }

    /**
     * 修改用户协议
     */
    public function userAgreement(Request $request)
    {
        $system = new System;
        return response()->json($system->editContent([
            'option'=>'user_agreement',
            'content'=>$request->input('content'),
        ]));
    }
}
